package com.bootdo.app;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.app.service.AssigningTaskService;
import com.bootdo.common.utils.AppR;
import com.bootdo.goods.domain.TGoodsAllocationAloneDO;
import com.bootdo.goods.domain.TGoodsAllocationDO;
import com.bootdo.goods.service.TGoodsAllocationAloneService;
import com.bootdo.goods.service.TGoodsAllocationService;
import com.bootdo.message.domain.MessageDO;
import com.bootdo.message.service.MessageService;
import com.bootdo.output.domain.OutStorageDO;
import com.bootdo.output.domain.OutStorageDetailDO;
import com.bootdo.output.service.OutStorageDetailService;
import com.bootdo.output.service.OutStorageService;
import com.bootdo.output.utils.packageOutType;
import com.bootdo.provider.domain.GoodsInfoDO;
import com.bootdo.staff.domain.StaffMenuRoleDO;
import com.bootdo.staff.service.StaffMenuRoleService;
import com.bootdo.task.domain.TTaskDO;
import com.bootdo.task.domain.TTaskDatilDO;
import com.bootdo.task.service.TTaskDatilService;
import com.bootdo.task.service.TTaskService;
import com.bootdo.task.util.PackageTaskType;

@Controller
@RequestMapping("api/end")
@CrossOrigin
public class AppEndController {
	@Autowired
	private StaffMenuRoleService staffMenuRoleService;
	@Autowired
	private AssigningTaskService assigningTaskService;
	@Autowired
	private TTaskDatilService tTaskDatilService;
	@Autowired
	private TTaskService TTaskService;
	@Autowired
	private OutStorageService outStorageService;
	@Autowired
	private TGoodsAllocationService tGoodsAllocationService;
	@Autowired
	private OutStorageDetailService outStorageDetailService;
	@Autowired
	private PackageTaskType packageTaskType;
	@Autowired
	private packageOutType packageOutType;
	@Autowired
	private MessageService MessageService;
	@Autowired
	private TGoodsAllocationAloneService tGoodsAllocationAloneService;

	/**
	 * 判断有无权限 显示任务的列表 更改任务状态 未执行
	 * 
	 * @param staffId
	 *            用户编号
	 * @param button_id
	 *            按钮编号
	 * @return 如果拥有权限 则返回
	 */
	@ResponseBody
	@GetMapping("/showTskList")
	public AppR showTskList(@RequestParam Long staffId, @RequestParam Long button_id) {



		// 接收到用户的编号 和按钮编号，查看该用户 有无进入出库按钮的权限
		AppR isOk = staffMenuRoleService.getIsRole(staffId, button_id);
		// 查看用户有无
		//
		if ((int) (isOk.get("code")) == 200) {
			// 出查询出信息\
			TTaskDO do1 = assigningTaskService.assigningTasks(staffId);
			if (do1 == null) {
				return AppR.ok("暂无任务");
			}
			Map<String, Object> map = new HashMap<>();
			map.put("tdTaskId", do1.getTaskId());
			List<TTaskDatilDO> datilDOs = tTaskDatilService.list(map);
			for (TTaskDatilDO tTaskDatilDO : datilDOs) {
				tTaskDatilDO = packageTaskType.packageTTaskDatilPojo(tTaskDatilDO);
			}
			do1.setDatils(datilDOs);// 获取任务详情
			// 状态更改为已发布

			// if(!do1.getTaskStatus().equals("12003")) {
			// TTaskDO do2 = new TTaskDO();
			// do2.setTaskId(do1.getTaskId());
			// do2.setTaskStatus("12002");//已发布的状态
			// TTaskService.update(do2);
			// }
			do1 = packageTaskType.packageTTaskPojo(do1);
			return AppR.ok(do1);
		} else {
			return AppR.error(1003, "无权访问");
		}

	}

	/**
	 * 为分拣任务添加标牌 更改状态 正在执行中
	 * 
	 * @param staffId
	 *            用户编号
	 * @param task_id
	 *            分拣任务编号
	 * @param scutcheon_id
	 *            标牌号
	 * @return 无返回
	 */
	@ResponseBody
	@PostMapping("/addScutcheonId")
	public AppR addScutcheonId(@RequestParam String staffId, @RequestParam Integer task_id,
			@RequestParam String scutcheon_id) {
		// 先查看该任务有无标牌
		TTaskDO do1 = TTaskService.get(task_id);
		if (do1.getTaskStatus().equals("12004")) {
			return AppR.ok("该任务已被完成，请重新领取任务");
		}
		if (do1.getTaskType() != null) {
			// 如果有标牌查看两次标牌数是否一致
			if (do1.getTaskType().equals(scutcheon_id)) {
				// 任务完成
				if (do1.getTaskStatus().equals("12005"))
					return AppR.ok("任务提交成功");
				TTaskDO d = new TTaskDO();
				d.setTaskId(task_id);
				Map<String, Object> map = new HashMap<>();
				map.put("tdTaskId", task_id);
				List<TTaskDatilDO> datilDOs = tTaskDatilService.list(map);
				for (TTaskDatilDO tTaskDatilDO : datilDOs) {
					if (!tTaskDatilDO.getTdStatus().equals("12004")) {
						return AppR.error("尚有任务未完成");
					}
				}
				d.setTaskStatus("12004");
				d.setTaskEtime(new Date());
				d.setTaskType(null);
				TTaskService.update(d);
				// 更改订单状态
				TTaskDO do2 = TTaskService.get(task_id);
				OutStorageDO do3 = new OutStorageDO();
				do3.setOutstorageId(Long.valueOf(do2.getTaskRemark()));
				do3.setOutstorageStatus("12205");
				outStorageService.update(do3);
				// 满足条件 任务提交
				return AppR.ok("任务提交成功");
			} else {
				return AppR.ok("与初始标牌不一致");
			}
		} else {
			// 如果没有标牌查看该标牌是否被使用
			TTaskDO isExist = TTaskService.scutcheonIsuse(scutcheon_id);// 获取标牌
			if (isExist == null) {
				TTaskDO tTaskDO = new TTaskDO();
				tTaskDO.setTaskName(staffId);
				tTaskDO.setTaskType(scutcheon_id);
				tTaskDO.setTaskId(task_id);
				tTaskDO.setTaskStatus("12003");// 任务执行中
				tTaskDO.setTaskStime(new Date());
				TTaskService.update(tTaskDO);
				// 更改订单状态
				// 根据任务获取订单状态更改

				OutStorageDO do3 = new OutStorageDO();
				do3.setOutstorageId(Long.valueOf(do1.getTaskRemark()));
				do3.setOutstorageStatus("12204");
				outStorageService.update(do3);
				return AppR.ok("标牌添加成功");

			} else {
				return AppR.ok("标牌已被使用");
			}
		}
	}

	/**
	 * 每一项任务清单的提交 扫码货位号 进行完成单项任务的分拣 传递 货位号 单号 用户 数量
	 * 
	 * @param staffId
	 *            用户编号
	 * @param task_id
	 *            任务编号
	 * @param freight_house_id
	 *            货位
	 * @return
	 */
	@ResponseBody
	@GetMapping("/execuSubTask")
	public AppR execuSubTask(@RequestParam Long staffId, @RequestParam Integer task_id,
			@RequestParam String freight_house_id) {
		// 扫码接口——员工编号 任务编号 货位编号
		// 进行货架状态的更改 进行货架数量的更改
		// 首先根据任务编号查询任务清单
		TTaskDO do1 = TTaskService.get(task_id);
		Map<String, Object> map = new HashMap<>();
		map.put("tdTaskId", task_id);
		List<TTaskDatilDO> datilDOs = tTaskDatilService.list(map);
		// 查看该货位编号所属的任务清单
		TTaskDatilDO datil = null;
		for (TTaskDatilDO tTaskDatilDO : datilDOs) {
			if (tTaskDatilDO.getTdInitalAllocation().equals(freight_house_id)) {
				datil = tTaskDatilDO;
			}
		}

		Integer commodity_number = datil.getTdNumber();
		datil.setTdNumber(commodity_number);
		datil.setTdTargetAllocation(commodity_number.longValue());// 最终出库数量
		datil.setTdStatus("12004");// 已完成
		// 对货位进行减少
		// 根据货位找到对应货位编号
		Map<String, Object> allocationMap = new HashMap<>();
		allocationMap.put("allocationNumber", freight_house_id);
		TGoodsAllocationDO allocation = tGoodsAllocationService.list(allocationMap).get(0);// 根据货位查询信息
		// 根据货位编号查询出货位
		if(!allocation.getAllocationType().equals("122003")) {
			TGoodsAllocationDO allocationDO = new TGoodsAllocationDO();
			allocationDO.setAllocationId(allocation.getAllocationId());
			allocationDO.setGoodsNumber(allocation.getGoodsNumber() - commodity_number);
			String lock = allocation.getRemark();
			Integer lockNumber = new Integer(lock) - commodity_number;
			if (lockNumber < 0)
				lockNumber = 0;
			allocationDO.setRemark(lockNumber.toString());
			tGoodsAllocationService.update(allocationDO); // 修复货位
			// 更改状态
			Integer ok = tTaskDatilService.update(datil);
			if (ok != 1)
				return AppR.error("系统错误");
			// 返回该任务清单 按照完成度排序 
		}
		
		map.put("sort", "td_status");
		map.put("order", "asc");
		datilDOs = tTaskDatilService.list(map);
		List<TTaskDatilDO> d = tTaskDatilService.list(map);
		for (TTaskDatilDO tTaskDatilDO : d) {
			tTaskDatilDO = packageTaskType.packageTTaskDatilPojo(tTaskDatilDO);
		}
		do1.setDatils(d);// 获取任务详情
		do1 = packageTaskType.packageTTaskPojo(do1);
		AppR r = new AppR();
		r.put("code", 200);
		r.put("msg", "成功");
		r.put("data", do1);

		return r;
	}

	/**
	 * 异常接口
	 * 
	 * @param task_id
	 *            任务号
	 * @param commodity_number
	 *            商品数量
	 * @param freight_house_id
	 *            货位
	 * @param staffId
	 *            用户
	 * @param errorType
	 *            错误类型
	 * @return
	 */
	@ResponseBody
	@PostMapping("/abnormal")
	public AppR abnormal(@RequestParam Integer task_id, @RequestParam Integer commodity_number,
			@RequestParam String freight_house_id, @RequestParam Long staffId, @RequestParam String errorType) {
		// 查询其他货位还有没有能分拣的数量
		// 如果没有就此罢休 有几个要几个
		// 如果有则查看 分拣位置有没有 有则
		// 有 生成分拣详情
		// 无 生成调仓任务
		// 异常上报
		// 判断商品 残缺？
		// 残 查找到供货商的残缺位置 返回货位号 放入物品
		// 缺 无

		TTaskDO do1 = TTaskService.get(task_id);
		// 获取任务
		Map<String, Object> map = new HashMap<>();
		map.put("tdTaskId", task_id);
		List<TTaskDatilDO> datilDOs = tTaskDatilService.list(map);
		// //查看该货位编号所属的任务清单
		TTaskDatilDO datil = null;
		for (TTaskDatilDO tTaskDatilDO : datilDOs) {
			if (tTaskDatilDO.getTdInitalAllocation().equals(freight_house_id)) {
				datil = tTaskDatilDO;// 获取哪一个详情
			}
		}
		// //查看 除了此货位外的其他货位
		// //获取商品编号
		Integer quantity = 0;// 仓库总量
		Integer sortingNumber = 0;// 分拣区总量
		Long goodId = datil.getTdGid();// 商品
		Integer number = (datil.getTdNumber() - commodity_number);// 所需要的数量
		Long providerId = do1.getTaskSupplierId();// 供货商id
		Integer num = 0;// 查询分拣位数量
		//
		// //更改货物最终分拣数量
		TTaskDatilDO d = new TTaskDatilDO();
		d.setTdId(datil.getTdId());
		d.setTdStatus("12004");// 已完成
		d.setTdTargetAllocation(commodity_number.longValue());
		//
		// //获取货物的单位
		Map<String, Object> outStorageDetail = new HashMap<>();
		outStorageDetail.put("outId", do1.getTaskRemark());
		outStorageDetail.put("datilGoodsid", goodId);// 商品编号
		List<OutStorageDetailDO> detailDOs = outStorageDetailService.list(outStorageDetail);
		OutStorageDetailDO detailDO = detailDOs.get(0);
		// //查询所有拥有此商品的货位
		Map<String, Object> GId = new HashMap<>();
		GId.put("goodsId", goodId);
		GId.put("sort", "(goods_number-remark)");// 分拣区减去
		GId.put("order", "asc");
		GId.put("allocationType", "122001");// 分拣位
		GId.put("allocationStatus", "120003");// 有货状态
		GId.put("providerId", providerId);// 供应商
		List<TGoodsAllocationDO> sortingAllocation = tGoodsAllocationService.list(GId);// 查询所有的分拣位有次商品的
		for (TGoodsAllocationDO place : sortingAllocation) {
			Integer goodNumber = place.getGoodsNumber();// 货位的商品数量
			Integer remarkNumber = new Integer(place.getRemark());// 分拣锁定数量
			sortingNumber += (goodNumber - remarkNumber); // 计算分拣区数量 减去分拣锁定位置
		}
		if (number > sortingNumber) {// 如果分拣区数量小于所需数量
			// // 调用调仓接口
			
			// 状态更改为 12005
			GId.put("allocationType", "122002");// 分拣位
			sortingAllocation = tGoodsAllocationService.list(GId);// 查询所有的分拣位有次商品的
			Integer goodNumber = 0;
			for (TGoodsAllocationDO place : sortingAllocation) {
				goodNumber += place.getGoodsNumber();// 货位的商品数量
			}
			Integer required = quantity - sortingNumber;// 所需数量
			Integer changeNumber = 0;// 需要调仓的数量
			if (goodNumber != 0) {
				if (goodNumber >= required) {
					changeNumber = required;
				} else {
					changeNumber = goodNumber;
				}
			}
		} else {
			List<String> datilId = new ArrayList<>();// 总共的分拣位编号
			List<Integer> datilNumberInte = new ArrayList<>(); // 每个分拣位所拥有的数量
			List<Integer> remarkNumberInte = new ArrayList<>(); // 每个分拣位锁定数量
			List<Long> datilIdNumber = new ArrayList<>(); // 每个分拣位位置编号
			int count = 0;
			for (int i = 0; i < sortingAllocation.size(); i++) {

				Long getAllID = sortingAllocation.get(i).getAllocationId();
				Integer goodNumber = sortingAllocation.get(i).getGoodsNumber();
				Integer remarkNumber = new Integer(sortingAllocation.get(i).getRemark());
				String getAllocationId = sortingAllocation.get(i).getAllocationNumber();
				if (goodNumber - remarkNumber <= 0) {
					continue;
				}
				num += (goodNumber - remarkNumber);
				datilId.add(count, getAllocationId);
				datilNumberInte.add(count, goodNumber);
				remarkNumberInte.add(count, remarkNumber);
				datilIdNumber.add(count, getAllID);
				count++;
				if (num >= number) {// 如果当前货架累计总数大于等于需要分拣的数量 break
					break;
				}
			}
			Integer sumNumber = 0; // 除去最后一个货位其他货位需要拿的数量
			for (int i = 0; i < datilNumberInte.size() - 1; i++) {
				sumNumber += datilNumberInte.get(i);
			}
			// 获取最后一个仓位需要拿多少
			Integer last = number - sumNumber;
			datilNumberInte.add(datilNumberInte.size() - 1, last);// 设置最后一个
			// 生成任务详细
			for (int i = 0; i < datilId.size(); i++) {
				TTaskDatilDO datilDO = new TTaskDatilDO();
				datilDO.setTdGid(goodId);// 商品编号
				datilDO.setTdInitalAllocation(datilId.get(i));
				datilDO.setTdNumber(datilNumberInte.get(i));
				datilDO.setTdStatus("12005");
				datilDO.setTdUnit(detailDO.getDatilPricingManner());
				datilDO.setTdTaskId(Long.valueOf(task_id.toString()));
				tTaskDatilService.save(datilDO);
				// 锁定货位数量
				TGoodsAllocationDO allocationDO = new TGoodsAllocationDO();
				Integer r = remarkNumberInte.get(i);
				r += datilNumberInte.get(i);// 获取原来分拣位数量 然后加上再次锁定的数量
				allocationDO.setAllocationId(datilIdNumber.get(i));
				allocationDO.setRemark(r.toString());
				tGoodsAllocationService.update(allocationDO);
			}
		}
		Object object = null;
		// //异常上报m
		String msg = "商品实际数量与库存数量不符";
		if (errorType.equals("c")) {
			GId.clear();
			// 查询残次品仓位获取编号
			GId.put("allocationType", "122004");// 残次品位置
			GId.put("providerId", providerId);// 有货状态
			TGoodsAllocationDO allocationDO = tGoodsAllocationService.list(GId).get(0);
			msg = "货物残缺，货位存放至退货货位"+ allocationDO.getAllocationNumber();
			GId.clear();
			//根据货位编号查询 特殊货位里有无该商品 如果有 就修改 如果没有 就添加
			GId.clear();
			GId.put("allocationNumber", allocationDO.getAllocationNumber());//货位编号
			GId.put("goodsId", goodId);
			List<TGoodsAllocationAloneDO> allocationAloneDOs = tGoodsAllocationAloneService.list(GId);
			if(allocationAloneDOs.size()<=0) {
				TGoodsAllocationAloneDO tGoodsAllocationAloneDO = new TGoodsAllocationAloneDO() ;
				String str = UUID.randomUUID().toString();
				tGoodsAllocationAloneDO.setId(str);
				tGoodsAllocationAloneDO.setAllocationNumber(allocationDO.getAllocationNumber());
				tGoodsAllocationAloneDO.setGoodsId(goodId);
				tGoodsAllocationAloneDO.setGoodsNumber(number);
				tGoodsAllocationAloneDO.setCreateDate(new Date());
				tGoodsAllocationAloneDO.setGoodsStandard(datil.getTdUnit());
				tGoodsAllocationAloneService.save(tGoodsAllocationAloneDO);
			}else {
				TGoodsAllocationAloneDO tGoodsAllocationAloneDO = allocationAloneDOs.get(0) ;
				TGoodsAllocationAloneDO tGoodsAllocationAlone = new TGoodsAllocationAloneDO() ;
				tGoodsAllocationAlone.setId(tGoodsAllocationAloneDO.getId());
				tGoodsAllocationAlone.setGoodsNumber(tGoodsAllocationAloneDO.getGoodsNumber() - number);
				tGoodsAllocationAloneService.update(tGoodsAllocationAlone);
			}
			object = allocationDO.getAllocationNumber();
		}
		MessageDO message = new MessageDO();
		
		message.setCreateTime(new Date());
		message.setMessageContent(msg);
		message.setMessageTitle("分拣异常");
		message.setSender(staffId);
		message.setReceiver(29L);
		MessageService.save(message);
		return AppR.ok(object);
	}

	/**
	 * 提交任务
	 * 
	 * @param staffId
	 *            用户编号
	 * @param task_id
	 *            分拣任务编号
	 * @return
	 */
	// @ResponseBody
	// @GetMapping("/completionOfTask")
	// public AppR completionOfTask (@RequestParam String staffId,@RequestParam
	// Integer task_id ) {
	// //根据订单号判断所有任务是不是都完成 或者等待
	// TTaskDO do1 =new TTaskDO();
	// do1.setTaskId(task_id);
	// Map<String, Object> map = new HashMap<>();
	// map.put("tdTaskId",task_id);
	// List<TTaskDatilDO> datilDOs = tTaskDatilService.list(map);
	// for (TTaskDatilDO tTaskDatilDO : datilDOs) {
	// if(tTaskDatilDO.getTdStatus().equals("12001")) {
	// return AppR.error("尚有任务未完成");
	// }
	// }
	// do1.setTaskStatus("12004");
	// do1.setTaskEtime(new Date());
	// TTaskService.update(do1);
	// //更改订单状态
	// TTaskDO do2 = TTaskService.get(task_id);
	// OutStorageDO do3 = new OutStorageDO();
	// do3.setOutstorageId(Long.valueOf(do2.getTaskRemark()));
	// do3.setOutstorageStatus("12205");
	// outStorageService.update(do3);
	// //满足条件 任务提交
	// return AppR.ok();
	// }
	/**
	 * 超时判断 任务提醒
	 * 
	 * @param staffId
	 *            用户编号
	 * @param task_id
	 *            任务编号
	 * @param time
	 *            时间数
	 * @return 返回任务清单
	 */

	// @ResponseBody
	// @GetMapping("/overtime")
	// public AppR overtime(@RequestParam String staffId,@RequestParam String
	// task_id,@RequestParam String time) {
	// return AppR.ok();
	// }
	/**
	 * 超时判断 回收任务
	 * 
	 * @param staffId
	 *            用户编号
	 * @param task_id
	 *            任务编号
	 * @return 返回任务清单
	 */
	@ResponseBody
	@GetMapping("/executionTimeout")
	public AppR executionTimeout(@RequestParam Long staffId, @RequestParam String task_id) {
		// 根据当前任务id 把所有无关的任务内容清空 进行任务回收 人员退出
		StaffMenuRoleDO staffMenuRoleDO = new StaffMenuRoleDO();
		staffMenuRoleDO.setStaffId(staffId);
		staffMenuRoleDO.setUserState(12102L); // 工作中
		staffMenuRoleDO.setSmenuId(4L);// 菜单编号 分拣
		int ok = staffMenuRoleService.updateUserState(staffMenuRoleDO);
		if (ok != 1)
			return AppR.error("系统错误");
		ok = 0;
		TTaskDO do1 = new TTaskDO();
		do1.setTaskName(null);// 人员制空
		do1.setTaskType(null);// 编号制空
		do1.setTaskId(new Integer(task_id));// 任务编号
		do1.setTaskStatus("12001");// 任务未发布
		do1.setTaskStime(null);// 时间制空
		ok = TTaskService.update(do1);
		if (ok == 1)
			return AppR.ok();
		return AppR.error("系统错误");
	}

	/**
	 * 交接扫码得数据
	 * 
	 * @param scutcheon_id
	 * @return
	 */
	@ResponseBody
	@GetMapping("/scanCode")
	public AppR scanCode(@RequestParam String scutcheon_id) {
		// 获取标牌码 根据标牌吗查询 等待出库的信息
		Map<String, Object> map = new HashMap<>();
		map.put("taskType", scutcheon_id);
		map.put("taskStatus", "12004");
		if (TTaskService.list(map).size() == 0) {
			return AppR.ok("该标牌所属订单不存在");
		}
		TTaskDO do1 = TTaskService.list(map).get(0);// 获取任务编号 根据订单号获取订单
		OutStorageDO do2 = outStorageService.get(Long.valueOf(do1.getTaskRemark()));
		map.clear();
		map.put("outId", do2.getOutstorageId());
		List<OutStorageDetailDO> detailDOs = outStorageDetailService.list(map);
		GoodsInfoDO g = null;
		OutStorageDetailDO d = null;
		for (OutStorageDetailDO DatilDO : detailDOs) {
			DatilDO = packageOutType.packageOutStorageDetailPojo(DatilDO);

			if ((DatilDO.getDatilEamount()) >= 0 && (DatilDO.getDatilEamount() < DatilDO.getDatilAmount())) {
				continue;
			}
			d = new OutStorageDetailDO();
			d.setDatilId(DatilDO.getDatilId());
			d.setDatilEamount(DatilDO.getDatilAmount());
			outStorageDetailService.update(d);
		}
		do2 = packageOutType.packageOutStoragePojo(do2);
		do2.setDos(detailDOs);
		return AppR.ok(do2);
	}

	/**
	 * 数量不符 异常提交
	 * 
	 * @param staffId
	 *            订单编号
	 * @param datil_id
	 *            详情编号
	 * @param good_number
	 *            商品数量
	 * @return
	 */
	@ResponseBody
	@PostMapping("/affirmDetailedList")
	public AppR affirmDetailedList(Long staffId, Long datil_id, Integer good_number) {
		OutStorageDetailDO d = new OutStorageDetailDO();
		d.setDatilId(datil_id);
		d.setDatilEamount(good_number);
		int ok = outStorageDetailService.update(d);
		MessageDO do1 = new MessageDO();
		do1.setCreateTime(new Date());
		do1.setMessageContent("交接数据不足");
		do1.setMessageTitle("交接异常");
		do1.setSender(staffId);
		do1.setReceiver(29L);
		MessageService.save(do1);
		// 发送异常
		AppR appR = new AppR();
		appR.put("code", 200);
		appR.put("msg", "操作成功，异常已上报");
		if (ok != 1) {
			appR.put("msg", "系统错误");
		}
		return appR;
	}

	/**
	 * 交接员扫码 完成交接
	 * 
	 * @param order_id
	 *            订单码
	 * @param staffId
	 *            用户编号
	 * @return
	 */
	@ResponseBody
	@GetMapping("/completeHandover")
	public AppR completeHandover(Long order_id, Long staffId) {
		// 订单完成 进行任务状态更改
		OutStorageDO do1 = new OutStorageDO();
		do1.setOutstorageId(order_id);
		do1.setOutstorageStatus("12206");
		do1.setOutstorageConnectId(staffId);
		int ok = outStorageService.update(do1);
		// 清除标牌号
		Map<String, Object> map = new HashMap<>();
		map.put("taskRemark", order_id);
		map.put("taskStatus", "12004");
		TTaskDO ttask = TTaskService.list(map).get(0);
		TTaskDO t = new TTaskDO();
		t.setTaskId(ttask.getTaskId());
		t.setTaskType("无");
		TTaskService.update(t);
		if (ok != 1) {
			return AppR.error("系统错误");
		}
		return AppR.ok();
		////
	}
}
